//
//  LCStudyViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/20.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCStudyViewController.h"
#import "LCStudyTableViewCell.h"
#import "LCTaskService.h"

#import "XWXAllXieYiController.h"

@interface LCStudyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic) NSMutableArray * dataArr;

@end

@implementation LCStudyViewController

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect headFrame = self.headView.frame;
    headFrame.size.height = 83*proW+21;
    self.headView.frame = headFrame;
    [self getStudyListData];
    
    [self setTableViewRefresh];
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getStudyListData];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getStudyListData];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

- (void)getStudyListData {
    [LCTaskService getStudyListWithClassID:@"10000000" Page:self.page SearchKey:@"" Block:^(BOOL isSuccess, NSArray<LCStudyModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90*proW + 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCStudyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCStudyTableViewCell"];
    cell.study = self.dataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LCStudyModel * study = self.dataArr[indexPath.row];
    XWXAllXieYiController *webVC = [XWXAllXieYiController new];
    webVC.tileStr = study.strtitle;
    webVC.urlStr = URLStudtWeb(study.newsid);
    webVC.isPushFrom = YES;
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}

@end
